﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using PhoneApp1.Resources;

namespace PhoneApp1
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        private bool Flag = false;
        Windows.Phone.Media.Capture.AudioVideoCaptureDevice avDevice = null;
        public MainPage()
        {
            InitializeComponent();

            // 用于本地化 ApplicationBar 的示例代码
            //BuildLocalizedApplicationBar();
        }
       
        private async void btnFlash_Click_1(object sender, RoutedEventArgs e)//一定不要忘记加async关键字（因为下面的代码里面用到了await关键字）
        {
            if (Flag)
            {
                //闪光灯已开启 则释放资源(关闭闪光灯)
                if (avDevice != null)
                {
                    avDevice.Dispose();
                    Flag = false;
                    
                }
            }
            else
            {
                var sensorLocation = Windows.Phone.Media.Capture.CameraSensorLocation.Back;
                avDevice = await Windows.Phone.Media.Capture.AudioVideoCaptureDevice.OpenAsync(sensorLocation, Windows.Phone.Media.Capture.AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First());
                //打开闪关灯
                var supportedCameraModes = Windows.Phone.Media.Capture.AudioVideoCaptureDevice.GetSupportedPropertyValues(sensorLocation, Windows.Phone.Media.Capture.KnownCameraAudioVideoProperties.VideoTorchMode);
                if (supportedCameraModes.ToList().Contains((UInt32)Windows.Phone.Media.Capture.VideoTorchMode.On))
                {
                    avDevice.SetProperty(Windows.Phone.Media.Capture.KnownCameraAudioVideoProperties.VideoTorchMode, Windows.Phone.Media.Capture.VideoTorchMode.On);
                    Flag = true;
                   
                }
            }
        }

        
    }
}